package com.clothing.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.clothing.dao.ArticleDao;
import com.clothing.dao.CommentDao;
import com.clothing.dao.MessageDao;
import com.clothing.entity.Article;
import com.clothing.entity.Comment;
import com.clothing.entity.Message;
import com.clothing.service.CommentService;
import com.clothing.utils.page.Filter;
import com.clothing.utils.page.Filter.Operator;
import com.clothing.utils.page.Page;
import com.clothing.utils.page.Pageable;

/**
 * @ClassName: CommentServiceImpl
 * @Description: TODO(Service)
 * @author Charles.Tao
 * @date 2017年9月6日 下午3:43:34
 * @version 1.0
 */
@Service
public class CommentServiceImpl extends BaseServiceImpl<Comment, Long>
        implements CommentService
{
    @Autowired
    private CommentDao commentDao;

    @Autowired
    private ArticleDao articleDao;
    
    @Autowired
    private MessageDao messageDao;

    @Override
    public Page<Comment> findCommentPage(Long articleId, Pageable pageable)
    {
        List<Filter> filters = new ArrayList<Filter>();
        filters.add(new Filter("article.id", Operator.eq, articleId));
        filters.add(new Filter("parent", Operator.isNull, null));
        pageable.setFilters(filters);

        return findPage(pageable);
    }

    @Override
    public void comment(Comment comment, Article article)
    {
        Message message = new Message();
        message.setContent(comment.getContent());
        message.setSender(comment.getCreator());
        message.setReceiver(article.getCreator());
        message.setType(Message.Type.comment);
        message.setFkId(article.getId());
        messageDao.persist(message);
        commentDao.persist(comment);
        articleDao.merge(article);
    }
}
